package com.rimi.controller;

import com.rimi.bean.User;
import com.rimi.service.UserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: Ecsystem
 * @Date: 2018/9/28 15:40
 * @Author: Mr.GuoQiang
 * @Description:
 */

@Controller
public class UserController {
    @Autowired
    private UserService userService;


    @RequestMapping("/registerPage")
    public String index(HttpServletRequest request) throws Exception {
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        String password = request.getParameter("pwd");

        System.out.println("js加密:"+password);
        String phone = request.getParameter("phone");

        User user = new User();
        user.setUsername(username);
        user.setPassword(DigestUtils.md5Hex(password));
        user.setPhone(phone);

        System.out.println("后台加密:"+user.getPassword());

        User user1 = userService.selectByUsername(username);
        if (user1 != null){
            System.out.println("已注册，直接登录");
            return "/login";
        }

        user.setStatus("未登录");
        int insert = userService.insertSelective(user);
        if (insert > 0){
            System.out.println("注册成功");
            return "/login";
        }
        System.out.println("注册失败");
        return "/register";
    }

    @RequestMapping("/register")
    public String register(){
        return "register";
    }

    @RequestMapping("/login")
    public String login(){
        return "login";
    }


    @RequestMapping("/loginPage")
    public String loginPage(Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        String username = request.getParameter("username");
        String password = request.getParameter("password");

        System.out.println(username+":"+password);
        //

        User user = userService.selectByUsername(username);
        System.out.println("password:"+user.getPassword()+":"+DigestUtils.md5Hex(password));
        if (user != null){
            System.out.println("账户存在");
            if (DigestUtils.md5Hex(password).equals(user.getPassword())){
                System.out.println("密码验证通过");
                model.addAttribute("account", username);
//              response.getWriter().append("/index");
//			    request.getRequestDispatcher("/index").forward(request, response);

                return "/index";
            }else {
                System.out.println("密码错误");
                response.getWriter().append("密码错误");
                return "/login";
            }
        }else {
            response.getWriter().append("账号不存在");
            return "/register";
        }

    }

}
